﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace ZLDC.Common.Estate
{
    public class _Cookie
    {
        public static bool SetCookie(string strName, string strValue, int strDay)
        {
            try
            {
                HttpCookie cookie = new HttpCookie(strName);
                cookie.Expires = DateTime.Now.AddDays((double)strDay);
                cookie.Value = strValue;
                HttpContext.Current.Response.Cookies.Add(cookie);
                return true;
            }
            catch
            {
            }
            return false;
        }


        public static bool SetCookie(string strName, string strValue)
        {
            try
            {

                string startTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string endTime = DateTime.Now.ToString("yyyy-MM-dd ") + "23:59:59";

                string Minutes = DateDiff(Convert.ToDateTime(startTime), Convert.ToDateTime(endTime));
                int TotalMinutes = _Convert.ToInt(Minutes);

                HttpCookie cookie = new HttpCookie(strName);
                cookie.Expires = DateTime.Now.AddMinutes((int)TotalMinutes);
                cookie.Value = strValue;
                HttpContext.Current.Response.Cookies.Add(cookie);
                return true;
            }
            catch
            {
            }
            return false;
        }

        public static string GetCookie(string strName)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie != null)
            {
                return cookie.Value.ToString();
            }
            return "";
        }

        public static bool DelCookie(string strName)
        {
            try
            {
                HttpCookie cookie = new HttpCookie(strName);
                cookie.Expires = DateTime.Now.AddDays(-1.0);
                HttpContext.Current.Response.Cookies.Add(cookie);
                return true;
            }
            catch
            {
            }
            return false;
        }


        private static string DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            string dateDiff = null;
            try
            {
                TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
                TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
                TimeSpan ts = ts1.Subtract(ts2).Duration();
                dateDiff = (ts.Hours * 60 + ts.Minutes).ToString();

            }
            catch
            {

            }
            return dateDiff;
        }

    }
}
